package com.service;

import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.TRX_OUT;
import static com.def.GenericDef._NORMAL;

import java.sql.Timestamp;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IRetPrdInfoDao;
import com.icim.service.ICIMBaseService;
import com.model.Ret_prd_info;
import com.tx.XpremarkI.XpremarkI;
import com.tx.XpremarkI.XpremarkIA;
import com.tx.XpremarkI.XpremarkO;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpremarkService")
public class XpremarkService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpremarkService.class);
	private static String TX_NAME = "XPREMARK";

	private XpremarkI inTrx;
	private XpremarkO outTrx;
	private MqOpe mqOpe;

	private String gszMsgBuf = "";
	private Timestamp gEvtTimestamp;

	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;

	public String subMainProc(String strInMsg) {
		long rtnCode = _NORMAL;

		String outMsg = null;
		gszMsgBuf = "";
		gEvtTimestamp = Util.getCurrentTimestamp();
		inTrx = new XpremarkI();
		outTrx = new XpremarkO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(RETURN_CODE_SET_ERROR);
		mqOpe = new MqOpe();

		try {
			logger.info("InTrx is " + strInMsg);
			inTrx = (XpremarkI) mqOpe.inStr2Obj(strInMsg, inTrx);

			rtnCode = subMainProc2();
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
				outTrx.setRtn_mesg(gszMsgBuf);
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				retPrdInfoDao.flushCurSession();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logger.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = mqOpe.obj2Str(outTrx);
			logger.info(outMsg);
		}
		return outMsg;
	}

	private long subMainProc2() {
		char action_flg = inTrx.getAction_flg().charAt(0);
		switch (action_flg) {
		case 'A':
			addRemarkFnc();
			break;
		case 'R':
			removeRemarkFnc();
			break;
		default:
			break;
		}
		return 0;
	}

	private void addRemarkFnc() {
		gEvtTimestamp = Util.getCurrentTimestamp();
		StringBuffer buffer  = new StringBuffer("FROM Ret_prd_info where prd_seq_id in(");
		List<XpremarkIA> iaryList = inTrx.getIary();
		for (XpremarkIA iary : iaryList) {
			buffer.append("'").append(iary.getPrd_seq_id()).append("',");
		}
		buffer.delete(buffer.length()-1,buffer.length());
		buffer.append(")");
		List<Ret_prd_info> retPrdInfoList = retPrdInfoDao.find(buffer.toString());
		for(Ret_prd_info retPrdInfo : retPrdInfoList){
			retPrdInfo.setSht_ope_msg(inTrx.getRemark());
			retPrdInfo.setEvt_usr(inTrx.getEvt_usr());
			retPrdInfo.setEvt_timestamp(gEvtTimestamp);
			retPrdInfoDao.update(retPrdInfo);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		
	}
	private void removeRemarkFnc() {
		gEvtTimestamp = Util.getCurrentTimestamp();
		StringBuffer buffer  = new StringBuffer("FROM Ret_prd_info where prd_seq_id in(");
		List<XpremarkIA> iaryList = inTrx.getIary();
		for (XpremarkIA iary : iaryList) {
			buffer.append("'").append(iary.getPrd_seq_id()).append("',");
		}
		buffer.delete(buffer.length()-1,buffer.length());
		buffer.append(")");
		List<Ret_prd_info> retPrdInfoList = retPrdInfoDao.find(buffer.toString());
		for(Ret_prd_info retPrdInfo : retPrdInfoList){
			retPrdInfo.setSht_ope_msg(null);
			retPrdInfo.setEvt_usr(inTrx.getEvt_usr());
			retPrdInfo.setEvt_timestamp(gEvtTimestamp);
			retPrdInfoDao.update(retPrdInfo);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

}
